﻿using System;
using System.Collections.Generic;
using QFramework;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace ProjectIndieFarm
{
    public class ComputerSystem : AbstractSystem, IComputerSystem
    {
        public List<ComputerItem> Items { get; } = new List<ComputerItem>();

        #region IAcrhive
        /// <summary>
        /// 保存电脑系统数据
        /// </summary>
        public void SaveData()
        {
            foreach (var item in Items)
            {
                PlayerPrefs.SetFloat($"computer_item_{item.Name}_current_hours", item.CurrentHours.Value);
                PlayerPrefs.SetInt($"computer_item_{item.Name}_finished", item.Finished.Value ? 1 : 0);
            }
            PlayerPrefs.SetInt(nameof(Global.ComputerSystemUnlocked), Global.ComputerSystemUnlocked.Value ? 1 : 0);
        }
        /// <summary>
        /// 加载电脑系统数据
        /// </summary>
        public void LoadData()
        {
            foreach (var item in Items)
            {
                item.CurrentHours.Value = PlayerPrefs.GetFloat($"computer_item_{item.Name}_current_hours", item.CurrentHours.Value);
                item.Finished.Value = PlayerPrefs.GetInt($"computer_item_{item.Name}_finished", item.Finished.Value ? 1 : 0) == 1;
            }
            Global.ComputerSystemUnlocked.Value = PlayerPrefs.GetInt(nameof(Global.ComputerSystemUnlocked), 0) == 1;
        }
        /// <summary>
        /// 重置电脑系统数据
        /// </summary>
        public void ResetData()
        {
            foreach (var item in Items)
            {
                item.CurrentHours.Value = 0;
                item.Finished.Value = false;
            }
            Global.ComputerSystemUnlocked.Value = false;
        }
        #endregion


        protected override void OnInit()
        {
            var game1 = Add(new ComputerItem()
                .SetName("开发第一款游戏")
                .SetTotalHours(80)
                .OnFInish(() =>
                {
                    //如果游戏制作完成 每日产生随机收益
                    Global.Days.Register(value =>
                    {
                        int gameIncome = UnityEngine.Random.Range(5, 15);
                        Global.Coins.Value += gameIncome;
                        UIMessageQueue.Push($"第一款游戏收入: +${gameIncome}");
                    }).UnRegisterWhenDisabled(Global.Player);
                }));
            var game2 = Add(new ComputerItem()
                .SetName("开发第二款游戏")
                .SetTotalHours(120)
                .ShowCondition(_ => game1.Finished.Value)
                .OnFInish(() =>
                {
                    Global.Days.Register(value =>
                    {
                        int gameIncome = UnityEngine.Random.Range(10, 25);
                        Global.Coins.Value += gameIncome;
                        UIMessageQueue.Push($"第二款游戏收入: +${gameIncome}");
                    }).UnRegisterWhenDisabled(Global.Player);
                }));
            var game3 = Add(new ComputerItem()
                .SetName("开发毕设游戏")
                .SetTotalHours(240)
                .ShowCondition(_ => game2.Finished.Value)
                .OnFInish(() =>
                {
                    //TODO:游戏通关
                    ActionKit.Delay(2.0f, () =>
                    {
                        SceneManager.LoadScene("GamePass");
                    }).Start(Global.Player);
                }));
        }

        public ComputerItem Add(ComputerItem item)
        {
            Items.Add(item);
            return item;
        }
    }
}
